import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.Socket;
import java.util.Scanner;

public class TcpCli {
    Socket socket = null;
    public TcpCli(String serverIp,int serverPort) throws IOException {
        socket = new Socket(serverIp,serverPort);
    }
    public void start(){
        System.out.println("客户端启动!");
        try (InputStream inputStream = socket.getInputStream();
             OutputStream outputStream = socket.getOutputStream()){
            Scanner scannerIn = new Scanner(System.in);
            Scanner scanner = new Scanner(inputStream);
            PrintWriter printWriter = new PrintWriter(outputStream);

            while(true){
                System.out.print("->");
                String request = scannerIn.next();

                printWriter.println(request);
                printWriter.flush();
                if(!scanner.hasNext()){
                    break;
                }
            }

            String response =scanner.next();
            System.out.println(response);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
